package main

import (
	"encoding/json"
	"strings"
)

func webSocketMessageWork(c *Client, m []byte) {
	//MyPrintf("\nstart----------------------------------------------startAAA \n")
	//fmt.Printf("client message: %s\n", m)
	t := webSocketMessageCheckDataType(m)
	if t == 1 {
		go webSocketMessageJSON(c, m)
	} else {
		go webSocketMessageOther(c, m)
	}
	//messageReadBackClient(c, m)
	//MyPrintf("\nend ---------------------------------------------- endAAA \n")
}

func webSocketMessageCheckDataType(m []byte) int {
	s := strings.TrimSpace(string(m))
	//is json
	if strings.HasPrefix(s, "{") {
		return 1
	} else {
		return 2
	}
}

/*
	eg:{ "C":"send" ,"P":"/dev/xxx" ,"D":"hexfdfdfdfd" }
*/
func webSocketMessageJSON(c *Client, m []byte) {
	var j ClientJSONData
	err := json.Unmarshal(m, &j)
	if err != nil {
		MyPrintf("Problem decoding json. giving up. json:%v, err:%v\n", m, err)
		return
	}
	// see if we have this port open
	//MyPrintf("data c: %s ,p: %s, d: %s \n", j.C, j.P, j.D)
	messageDoWork(j, c, nil, 1)
}

func webSocketMessageOther(c *Client, m []byte) {
	MyPrintf("messageOther data: %s\n", m)
}
